package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WorkTicketInfoMapper;
import com.ruoyi.business.domain.WorkTicketInfo;
import com.ruoyi.business.service.IWorkTicketInfoService;

import javax.validation.Validator;

/**
 * 工作票Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class WorkTicketInfoServiceImpl  extends BaseServiceImpl implements IWorkTicketInfoService
{
    @Autowired
    private WorkTicketInfoMapper workTicketInfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询工作票
     * 
     * @param id 工作票主键
     * @return 工作票
     */
    @Override
    public WorkTicketInfo selectWorkTicketInfoById(Long id)
    {
        return workTicketInfoMapper.selectWorkTicketInfoById(id);
    }

    /**
     * 查询工作票列表
     * 
     * @param workTicketInfo 工作票
     * @return 工作票
     */
    @Override
    public List<WorkTicketInfo> selectWorkTicketInfoList(WorkTicketInfo workTicketInfo)
    {
        return workTicketInfoMapper.selectWorkTicketInfoList(workTicketInfo);
    }

    /**
     * 新增工作票
     * 
     * @param workTicketInfo 工作票
     * @return 结果
     */
    @Override
    public int insertWorkTicketInfo(WorkTicketInfo workTicketInfo)
    {
        workTicketInfo.setCreateTime(DateUtils.getNowDate());
        return workTicketInfoMapper.insertWorkTicketInfo(workTicketInfo);
    }

    /**
     * 修改工作票
     * 
     * @param workTicketInfo 工作票
     * @return 结果
     */
    @Override
    public int updateWorkTicketInfo(WorkTicketInfo workTicketInfo)
    {
        workTicketInfo.setUpdateTime(DateUtils.getNowDate());
        return workTicketInfoMapper.updateWorkTicketInfo(workTicketInfo);
    }

    /**
     * 批量删除工作票
     * 
     * @param ids 需要删除的工作票主键
     * @return 结果
     */
    @Override
    public int deleteWorkTicketInfoByIds(Long[] ids)
    {
        return workTicketInfoMapper.deleteWorkTicketInfoByIds(ids);
    }

    /**
     * 删除工作票信息
     * 
     * @param id 工作票主键
     * @return 结果
     */
    @Override
    public int deleteWorkTicketInfoById(Long id)
    {
        return workTicketInfoMapper.deleteWorkTicketInfoById(id);
    }

    @Override
    public String importData(List<WorkTicketInfo> userList, boolean updateSupport, String operName) {
    if (userList == null || userList.isEmpty()) {
        throw new ServiceException("导入工单信息数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
    List<String>failureNums = new ArrayList<>();

    int i = 1;
    ff:for (WorkTicketInfo user : userList) {
        try {
            i++;
            if (user.getAwardYear() == null || user.getAwardYear().equals("") || user.getEmployeeId() ==null || user.getEmployeeId().equals("") || !isAwardYearValid(user.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }

            // 验证是否存在这个工单信息
            WorkTicketInfo workTicketInfo = new WorkTicketInfo();
            workTicketInfo.setEmployeeId(user.getEmployeeId());

            List<WorkTicketInfo> selectWorkTicketInfoList = workTicketInfoMapper.selectWorkTicketInfoList(workTicketInfo);


            if (selectWorkTicketInfoList.isEmpty()) {
                // 验证数据
                BeanValidators.validateWithException(validator, user);
                // 设置初始值
                user.setCreateTime(DateUtils.getNowDate());
                user.setCreateBy(operName);
                workTicketInfoMapper.insertWorkTicketInfo(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、工单 " + user.getTicketNumber() + " 导入成功");
            } else if (true) {
                // 验证数据
                WorkTicketInfo existingTicket = selectWorkTicketInfoList.get(0);
                BeanValidators.validateWithException(validator, user);
                user.setId(existingTicket.getId());
                user.setUpdateTime(DateUtils.getNowDate());
                user.setUpdateBy(operName);
                workTicketInfoMapper.updateWorkTicketInfo(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、工单 " + user.getTicketNumber() + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、工单 " + user.getTicketNumber() + " 已存在");
                failureNums.add(String.valueOf(i));
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、工单 " + user.getTicketNumber() + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
            failureNums.add(String.valueOf(i));
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }

    setCacheInfo("工作票信息",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
